package site.beanyon.now.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.beanyon.now.constant.ResponseEnum;
import site.beanyon.now.model.dto.HttpResponse;
import site.beanyon.now.model.dto.TimeCardDTO;
import site.beanyon.now.model.po.TimeCardPO;
import site.beanyon.now.service.TimeCardService;
import site.beanyon.now.service.UserService;
import site.beanyon.now.util.DateUtils;
import site.beanyon.now.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 时间卡片控制器
 */
@RestController
@RequestMapping("/time-card")
public class TimeCardController {
    @Autowired
    private TimeCardService timeCardService;

    @Autowired
    private UserService userService;

    /**
     * 获取时间卡片列表
     *
     * @param date 要获取的日期（yyyy-MM-dd 格式）
     * @return 时间戳所属的那一天的时间卡片列表
     */
    @GetMapping("/list")
    private HttpResponse listTimeCard(String date) {
        // date为空，默认获取今日时间卡片
        if (StringUtils.isEmpty(date)) {
            date = DateUtils.getDateByTimestamp(0L);
        }

        // 根据日期，获取当前用户的时间卡片
        List<TimeCardDTO> timeCardDTOList = this.timeCardService.listTimeCard(date);

        return new HttpResponse(ResponseEnum.SUCCESS, timeCardDTOList);
    }

    /**
     * 创建时间卡片
     *
     * @return 是否创建成功
     */
    @PostMapping("/create")
    private HttpResponse createTimeCard() {
        // 用于接收插入数据库后的时间卡片id
        TimeCardPO timeCardPO = new TimeCardPO();
        // 创建时间卡片
        boolean success = this.timeCardService.createTimeCard(timeCardPO);

        // 创建成功，返回时间卡片的id
        if (success) {
            return new HttpResponse(ResponseEnum.SUCCESS_CREATE_TIME_CARD, timeCardPO.getId());
        }

        return new HttpResponse(ResponseEnum.FAILED_CREATE_TIME_CARD, null);
    }

    /**
     * 删除时间卡片
     *
     * @param params 要删除的时间卡片id
     * @return 删除结果
     */
    @PostMapping("/delete")
    private HttpResponse deleteTimeCard(@RequestBody Map<String, Integer> params) {
        Integer cardId = params.get("cardId");

        // 要删除的时间卡片id不能为空
        if (cardId == null) {
            return new HttpResponse(ResponseEnum.FAILED_DELETE_TIME_CARD, null);
        }

        // 删除时间卡片
        boolean success = this.timeCardService.deleteTimeCard(cardId);

        if (success) {
            return new HttpResponse(ResponseEnum.SUCCESS_DELETE_TIME_CARD, null);
        }

        return new HttpResponse(ResponseEnum.FAILED_DELETE_TIME_CARD, null);
    }

    /**
     * 更新时间卡片的activity
     *
     * @param params 参数Map
     * @return 是否修改成功
     */
    @PostMapping("/update-activity")
    private HttpResponse updateTimeCardActivity(@RequestBody Map<String, String> params) {
        String cardId = params.get("cardId");
        String activity = params.get("activity");

        // 对cardId判空
        if (StringUtils.isEmpty(cardId)) {
            return new HttpResponse(ResponseEnum.REQUIRED_PARAM_NOT_EXISTS, "时间卡片id为空");
        }

        // 对activity判空
        if (StringUtils.isEmpty(activity)) {
            return new HttpResponse(ResponseEnum.ACTIVITY_IS_NULL, null);
        }

        // 判断activity是否已经在数据库中存在
        if (this.timeCardService.activityExists(activity)) {
            return new HttpResponse(ResponseEnum.ACTIVITY_EXISTS, null);
        }

        // 更新时间卡片的activity
        boolean success = this.timeCardService.updateActivity(Integer.valueOf(cardId), activity);

        if (success) {
            return new HttpResponse(ResponseEnum.SUCCESS_UPDATE_ACTIVITY, null);
        }

        return new HttpResponse(ResponseEnum.FAILED_UPDATE_ACTIVITY, null);
    }
}
